<?php

namespace slinks\extensions\onecms\cms\joomla;

use slinks\extensions\onecms\core\model\Menu;
use slinks\extensions\onecms\core\model\MenuItem;
use slinks\extensions\onecms\core\model\MenuItemSeparator;

/**
 * Adapts Joomla Menu objects to internal Menu objects. 
 * 
 * @author Navid Mitchell
 * @Service
 */
class JoomlaMenuService implements \slinks\extensions\onecms\core\ICmsMenuService {

    /**
     * Get the menu for the given id. 
     * @param string $menuId the menu id.
     * @return Menu the menu for the given id. 
     */
    public function getMenu($menuId) {

        $jMenu = \JSite::getMenu();
        $jItems = $jMenu->getItems('menutype', $menuId);

        if (!isset($jItems)) {
            throw new \InvalidArgumentException('No Joomla menu found for id ' . $menuId);
        }

        $menuNodes = array();
        $menuItems = array();

        // all items including children are in $jItems
        foreach ($jItems as $jItem) {
            $item = null;
            if ($jItem->type == 'separator') {
                $item = new MenuItemSeparator();
            } else {
                $item = new MenuItem($jItem->name, $this->getLink($jMenu, $jItem));
                $item->setActive($this->isActive($jMenu, $jItem));
            }
            // only root level elements are stored in the menuNodes array. 
            if ($jItem->parent == 0) {
                $menuNodes[] = $item;
            } else {
                $parent = $menuItems[$jItem->parent];
                $parent->addChild($item);
            }
            // All menu Items keyed by their Jooomla Id. 
            $menuItems[$jItem->id] = $item;
        }

        return new Menu($menuId, $menuNodes);
    }

    private function isActive($menu, $menuItem) {
        $activeItem = $menu->getActive();
        // if menulink then use the menulink for active comparison.
        if ($menuItem->type == 'menulink') {
            $newItem = $menu->getItem($menuItem->queryParameters['Itemid']);
            if (isset($newItem)) {
                $menuItem = $newItem;
            }
        }
        return $activeItem->id == $menuItem->id;
    }

    /**
     * 
     * 
     * @param unknown_type $menu
     * @param unknown_type $menuItem
     */
    private function getLink($menu, $menuItem) {
        $alias = false;
        if ($menuItem->type == 'menulink') {
            $newItem = $menu->getItem($menuItem->queryParameters['Itemid']);
            if (isset($newItem)) {
                $menuItem = $newItem;
            }
        }

        $link = $menuItem->link;
        $id = $menuItem->id;
        switch ($menuItem->type) {

            case 'url':
                if ((strpos($link, 'index.php?') === 0) && (strpos($link, 'Itemid=') === false)) {
                    $link = $link . '&amp;Itemid=' . $id;
                }
                break;
            default :
                $router = \JSite::getRouter();

                $link = ($router->getMode() == JROUTER_MODE_SEF) ? 'index.php?Itemid=' . $id : $link . '&Itemid=' . $id;
                break;
        }
        return $link;
    }

}